﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizDjkOrderRouteDetail : CustBizObject
    {
        private CDmDjkOrderRouteDetail m_CDmDjkOrderRouteDetail = new CDmDjkOrderRouteDetail();
        private CDmDjkOrderRouteDetail oDmDjkOrderRouteDetail
        {
            get { return m_CDmDjkOrderRouteDetail; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDjkOrderRouteDetail()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkOrderRouteDetail.DJK_ORDER_ROUTE_DETAIL);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ROUTE_DETAIL_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加预定线路详情信息
        /// </summary>
        /// <param name="detailIds">详情信息编号</param>
        /// <param name="targType">目标类型</param>
        /// <param name="routeId">线路编号</param>
        /// <returns></returns>
        public string[] AddOrderRouteDetail(string detailIds, string targType, string routeId)
        {
            if (string.IsNullOrWhiteSpace(detailIds))
            {
                return new string[] { "False", "路线详情信息有误" };
            }

            if (string.IsNullOrWhiteSpace(targType))
            {
                return new string[] { "False", "路线详情类型信息有误" };
            }

            if (string.IsNullOrWhiteSpace(routeId))
            {
                return new string[] { "False", "线路信息错误" };
            }
            DataSet ds, dsRoute;
            using (CBizDjkOrderRoute objCBizDjkOrderRoute = new CBizDjkOrderRoute())
            {
                dsRoute = objCBizDjkOrderRoute.SelectById(routeId);

                if (dsRoute == null
                    || dsRoute.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "False", "线路信息错误" };
                }
            }

            DataSet dsDetail = new CDmDjkOrderRouteDetail();


            if (targType == "1")
            {
                using (CBizPushScenic objCBizPushScenic = new CBizPushScenic())
                {
                    ds = objCBizPushScenic.SelectByIds(detailIds);
                }

                if (ds == null
                    || ds.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "False", "路线详情类型信息有误" };
                }
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = dsDetail.Tables[0].NewRow();
                    decimal price = 0;
                    decimal.TryParse(ds.Tables[0].Rows[i]["SCENIC_PRICE"].ToString(),out price);

                    dr["ROUTE_DETAIL_ID"] = this.DataObj.NewID();
                    dr["ROUTE_ID"] = routeId;
                    dr["ROUTE_NME"] = dsRoute.Tables[0].Rows[0]["ORDER_ROUTE_NME"];
                    dr["DETAIL_INFO_ID"] = ds.Tables[0].Rows[i]["SCENIC_ID"];
                    dr["DETAIL_INFO_NME"] = ds.Tables[0].Rows[i]["SCENIC_NAME"];
                    dr["DETAIL_INFO_TYPE"] = 1;
                    dr["DETAIL_BELONG_DAY"] = 1;
                    dr["DETAIL_INFO_PIC"] = ds.Tables[0].Rows[i]["SCENIC_DEFAULT_PIC"];
                    dr["DETAIL_INFO_INTRO"] = ds.Tables[0].Rows[i]["CONTENT_FILE_URL"];
                    dr["DETAIL_INFO_PRICE"] = price;
                    dr["DETAIL_BUY_RULE"] = 0;
                    dr["DETAIL_BUY_LIMIT"] = 0;
                    dr["DETAIL_IS_SHOW"] = 1;
                    dr["DETAIL_IS_VALID"] = 1;

                    dsDetail.Tables[0].Rows.Add(dr);
                }
            }

            this.Update(dsDetail);

            return new string[] { "True", "添加成功" };
        }

        /// <summary>
        /// 获取自定义路线 景点列表
        /// </summary>
        /// <param name="routeid">自定义路线id</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-29
        public DataSet GetOrderRouteDetailList(string routeid)
        {
            if (string.IsNullOrEmpty(routeid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("routeid", ParameterDirection.Input, EDbType.String, 36, routeid));

            return this.DataObj.QueryBySQL(@"SELECT ROUTE_DETAIL_ID,ROUTE_ID,ROUTE_NME,DETAIL_INFO_ID,DETAIL_INFO_NME,DETAIL_INFO_PIC,DETAIL_INFO_INTRO FROM DJK_ORDER_ROUTE_DETAIL WHERE DETAIL_IS_SHOW=1 AND DETAIL_IS_VALID=1 AND ROUTE_ID=@routeid ", parmCollection);

        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="detailId"></param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-29
        public string UpdateStatus(string detailId)
        {
            if (string.IsNullOrEmpty(detailId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("detailId", ParameterDirection.Input, EDbType.String, 36, detailId));

            this.DataObj.QueryBySQL(@"UPDATE DJK_ORDER_ROUTE_DETAIL SET DETAIL_IS_SHOW='0',DETAIL_IS_VALID='0' ROUTE_DETAIL_ID=@detailId ", parmCollection);

            return "OK";
        }
    }
}
